package com.jike.shop.cloud.user;

import com.jike.shop.cloud.IUserService;
import com.jike.shop.cloud.query.UserQueryBo;
import com.jike.shop.cloud.user.dto.Authentication;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @DubboReference(version = "1.0.0", group = "jike-shop")
    private IUserService iUserService;

    @PostMapping(value = "/login")
    public String login(@RequestParam UserQueryBo userQueryBo) {
        Authentication loginResult = iUserService.login(userQueryBo.getUsername(), userQueryBo.getPassword());
        if (userQueryBo.getTokenType().equals(0)) {
            //返回accessToken
            return loginResult.getAccessToken();
        } else {
            //返回刷新Token
            return loginResult.getRefreshToken();
        }
    }

    @PostMapping(value = "/refreshToken")
    public String refreshToken(@RequestParam UserQueryBo userQueryBo) {
        Authentication refreshResult = iUserService.refresh(userQueryBo.getRefreshToken());
        return refreshResult.getRefreshToken();
    }
}
